<?php

	function requestPOST($request) {
		$requestJSON = json_decode(utf8_decode($request->postArgs['request']));
		
		$serviceRequest = new Request();
		
		$serviceRequest->JSONToObject($requestJSON);
		$session = unserialize($_SESSION['smarthotel']['session']);
		$user = $session->getUser();
		$serviceRequest->setRoomGuestId($user->getRoomGuestId());

		$requestBusiness = BusinessFactory::factory('RequestBusiness');
		$result = $requestBusiness->save($serviceRequest);

		if ($result != -1) {
			
			header("Status: 201 Created", true, 201);
			header("Location: " . $request->getActualUrl() . $result);
		}
		else {
			header("Status: 500 Internal Server Error", true, 500);
		}
	}
	
	function requestDELETE($request) {
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$serviceRequest = new Request();
			$serviceRequest->setRequestId($request->getArgs['id']);
									
			$requestBusiness = BusinessFactory::factory('RequestBusiness');
			$result = $requestBusiness->deleteRequest($serviceRequest);
			
			if ($result) {
				header("Status: 200 Ok", true, 200);
				//TODO checar qual location seria retornado
				header("Location: " . $request->getActualUrl());
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}	
	
	function requestacceptPUT($request) {
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$serviceRequest = new Request();
			$serviceRequest->setRequestId($request->getArgs['id']);
			
			$session = unserialize($_SESSION['smarthotel']['session']);
			$user = $session->getUser();
			
			$employee = new Employee();
			$employee->setEmployeeId($user->getEmployeeId());
			
			$serviceRequest->setEmployee($employee);

			$requestBusiness = BusinessFactory::factory('RequestBusiness');
			$result = $requestBusiness->updateRequest($serviceRequest);
			
			if ($result) {
				header("Status: 200 Ok", true, 200);
				//TODO checar qual location seria retornado
				header("Location: " . $request->getActualUrl());
			}
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 400 Bad Request", true, 400);
		}
	}
	
	/*Pega os requests do user logado*/
	function requestGET($request) {
		
		$session = unserialize($_SESSION['smarthotel']['session']);
		$user = $session->getUser();
			
		$requestBusiness = BusinessFactory::factory('RequestBusiness');
		$result = $requestBusiness->selectRequestsByUserId($user->getUserId());
		
		if ($result != NULL) {
		
			if(is_array($result) && sizeof($result) > 0) {
					
				header("Status: 200 Success", true, 200);
				
			} else {
				header("Status: 404 Not Found", true, 404);
			} 
		}
		else {
			header("Status: 500 Internal Server Error", true, 500);
		} 
		
		return $result;
	}
	
	function requestitemsGET($request) {
		
		$requestService = new Request();
		$requestService->setRequestId($request->getArgs['id']);
			
		$requestItemBusiness = BusinessFactory::factory('RequestItemBusiness');
		$result = $requestItemBusiness->selectRequestItemsByRequestId($requestService->getRequestId());
		
		if ($result != NULL) {
		
			if(is_array($result) && sizeof($result) > 0) {
					
				header("Status: 200 Success", true, 200);
				
			} else {
				header("Status: 404 Not Found", true, 404);
			} 
		}
		else {
			header("Status: 500 Internal Server Error", true, 500);
		} 
		
		return $result;
	}

?>